package com.shop.shoporder.service.impl;


import com.shop.shoporder.common.InterfaceVer;
import com.shop.shoporder.entity.GroupbuyGoodInfo;
import com.shop.shoporder.entity.Spec;
import com.shop.shoporder.entity.order.Cart;
import com.shop.shoporder.entity.order.CartList;
import com.shop.shoporder.entity.order.Order;
import com.shop.shoporder.entity.order.UserNo;
import com.shop.shoporder.mapper.GroupbuyGoodMapper;
import com.shop.shoporder.mapper.SpecMapper;
import com.shop.shoporder.mapper.order.CartMapper;
import com.shop.shoporder.service.CartService;
import com.shop.shoporder.service.UserNoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("cartService")
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    SpecMapper specMapper;
    @Autowired
    UserNoService userNoService;
    @Autowired
    GroupbuyGoodMapper groupbuyGoodMapper;
    @Override
    public int deleteByPrimaryKey(Integer cartId) {
        return cartMapper.deleteByPrimaryKey(cartId);
    }

    @Override
    public int insert(Cart record) {
        return cartMapper.insert(record);
    }

    @Override
    public int insertSelective(Cart record) {
        return cartMapper.insertSelective(record);
    }

    @Override
    public Cart selectByPrimaryKey(Integer cartId) {
        return cartMapper.selectByPrimaryKey(cartId);
    }

    @Override
    public int updateByPrimaryKeySelective(Cart record) {
        return cartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Cart record) {
        return cartMapper.updateByPrimaryKey(record);
    }

    @Override
    public int upCartLogType(List<String> cartIdsList, int logisticsType) {
        return cartMapper.upCartLogType(cartIdsList,logisticsType);
    }


    @Override
    public List<CartList> selectOrderList(Order order) {
        return cartMapper.selectOrderList(order);
    }

    @Override
    public List<Cart> getOrderCommentGoodsList(Map<String, String> map, HttpServletRequest request) throws Exception {
        int userNoId=0;
        int type=0;
        int productId=0;
        List<Cart> list=new ArrayList<>();
        List<String> userNoIds=new ArrayList<>();
        if(map!=null) {
            userNoId = InterfaceVer.VerNumber(map.get("userNoId"));
            type = InterfaceVer.VerNumber(map.get("type"));
            productId = InterfaceVer.VerNumber(map.get("productId"));
            list=cartMapper.getOrderCommentGoodsList(userNoId,type,productId);
            if(list!=null&&list.size()>0){
                for (Cart cart:list) {
                    String userNoIdStr=cart.getUsernoId()==null?"":cart.getUsernoId()+"";
                    if(!"".equals(userNoIdStr)){
                        userNoIds.add(userNoIdStr);
                    }
                }
                List<String> userNoIdsList = new ArrayList<>(userNoIds);
                String noList=org.apache.commons.lang.StringUtils.join(userNoIdsList.toArray(),",");
                List<UserNo> jsonObject1 = userNoService.getUserNoList(noList);
                if (jsonObject1 != null&&jsonObject1.size()>0) {
                    for (UserNo userNo:jsonObject1) {
                        int userNoIdInt=userNo.getId();
                        String userNoStr=userNo.getNo();
                        for (Cart cart:list) {
                            if(cart.getUsernoId()==userNoIdInt){
                                cart.setUsernoName(userNoStr);
                            }
                        }
                    }
                }
            }
            return list;
        }else {
            return null;
        }
    }


    @Override
    public List<CartList> selectCartListByOrderNo(int usernoId, String orderNo) {
        return cartMapper.selectCartListByOrderNo(usernoId,orderNo);
    }


//    @Override
//    public Cart selectCartByProductIDAndGuigeID(Cart record) {
//        return cartMapper.selectCartByProductIDAndGuigeID(record);
//    }

    @Override
    @Transactional
    public Integer addCart(Map<String, String> map, HttpServletRequest request) throws Exception {
        int userNoId=0;
        int productId=0;
        int guigeId=0;
        int productCount=0;
        int shopId=0;
        int groupType=0;
        int res=0;
        int resCartId=0;
        Cart cart=new Cart();
        double price=0;
        int uLimit=0;
        double groupPrice=0;
        if(map!=null){
            userNoId= InterfaceVer.VerNumber(map.get("userNoId"));
            productId= InterfaceVer.VerNumber(map.get("productId"));
            guigeId= InterfaceVer.VerNumber(map.get("guigeId"));
            productCount= InterfaceVer.VerNumber(map.get("productCount"));
            shopId= InterfaceVer.VerNumber(map.get("shopId"));
            groupType= InterfaceVer.VerNumber(map.get("groupType"));
            //System.out.println("userNoId:"+userNoId+" productId:"+productId+" guigeId:"+guigeId+" productCount:"+productCount+" shopId:"+shopId);

            //根据用户 产品 规格 店铺 查询是否添加了购物车
            Cart cart1=new Cart();
            cart1.setUsernoId(userNoId);
            cart1.setProductId(productId);
            cart1.setGuigeId(guigeId);
            cart1.setShopId(shopId);
            cart=cartMapper.selectCartIsExist(cart1);
            if(cart!=null){
                int productCountTemp=0;
                Spec spec=specMapper.selectByPrimaryKey(guigeId);

                uLimit=spec.getUlimit()==null?0:spec.getUlimit();
                if(groupType==1){
                    GroupbuyGoodInfo groupbuyGoodInfo =groupbuyGoodMapper.getGoodInfoByGoodIdAndSpecId(productId,guigeId);
                    if(groupbuyGoodInfo!=null){
                        price=groupbuyGoodInfo.getBuyonePrice()==null?0:groupbuyGoodInfo.getBuyonePrice();
                    }
                }else if(groupType==2){
                    GroupbuyGoodInfo groupbuyGoodInfo =groupbuyGoodMapper.getGoodInfoByGoodIdAndSpecId(productId,guigeId);
                    if(groupbuyGoodInfo!=null){
                        price=groupbuyGoodInfo.getGroupbyPrice()==null?0:groupbuyGoodInfo.getGroupbyPrice();
                    }
                }else {
                    price=spec.getPrice()==null?0:spec.getPrice();
                }

                int cartCount=cart.getProductCount()==null?0:cart.getProductCount();
                productCountTemp=productCount;
//                if(cartCount>0){
////                    productCountTemp=cartCount;
//                    productCountTemp=productCount;
//                }else {
//                    productCountTemp=productCount;
//                }
                //int productCountTemp=(cart.getProductCount()==null?0:cart.getProductCount())+productCount;
                if(productCountTemp>0){
//                    if(uLimit>0){
//
//                    }
//                    if(productCountTemp>uLimit){
//                        productCountTemp=uLimit;
//                    }
                    cart.setProductCount(productCountTemp);
                    cart.setRemark(price+"");
                    cart.setRemark2(groupType+"");
                    res=cartMapper.updateByPrimaryKeySelective(cart);
                    if(res<=0){
                        throw new RuntimeException();
                    }
                    resCartId=cart.getCartId();
                }
            }else {
//                if(productCount>uLimit){
//                    productCount=uLimit;
//                }
                cart1.setProductCount(productCount);
                cart1.setCartStatus(501);
                cart1.setLogisticsType(1);
                cart1.setRemark(price+"");
                cart1.setRemark2(groupType+"");
                List<Spec> specs=specMapper.selectActivityGoods();
                for (Spec spec:specs) {
                    int guigeidSPEC=spec.getId()==null?-1:spec.getId();
                    if(guigeidSPEC==guigeId){
                        cart1.setRemark1("1");
                        break;
                    }
                }
                res=cartMapper.insertSelective(cart1);
                if(res<=0){
                    throw new RuntimeException();
                }
                resCartId=cart1.getCartId();
            }
            if(res>0){
                return resCartId;
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }

    @Override
    @Transactional
    public Boolean editCart(Map<String, String> map, HttpServletRequest request) throws Exception {
        int cartId=0;
        int productCount=0;
        int res=0;
        int userNoId=0;
        Cart cart=new Cart();
        if(map!=null){
            cartId= InterfaceVer.VerNumber(map.get("cartId"));
            productCount= InterfaceVer.VerNumber(map.get("productCount"));
            userNoId= InterfaceVer.VerNumber(map.get("userNoId"));
            cart=cartMapper.selectByPrimaryKey(cartId);
            //System.out.println("cart:"+cart.toString());
            if(cart!=null){
                int orderUserNoId=cart.getUsernoId()==null?0:cart.getUsernoId();
                if(orderUserNoId==userNoId) {
                    int productCountTemp = (cart.getProductCount() == null ? 0 : cart.getProductCount()) + productCount;
                    //System.out.println("productCountTemp:"+productCountTemp);
                    if (productCountTemp > 0 && productCountTemp != productCount) {
                        cart.setProductCount(productCountTemp);
                        res = cartMapper.updateByPrimaryKeySelective(cart);
                    }
                }else {
                    return false;
                }
            }else {
                return false;
            }
            if(res>0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    @Transactional
    public Boolean deleteCart(Map<String, String> map, HttpServletRequest request) throws Exception {
        Integer cartId=0;
        Integer userNoId=0;
        Integer res=0;
        Cart cart=new Cart();
        if(map!=null){
            userNoId= InterfaceVer.VerNumber(map.get("userNoId"));
            cartId= InterfaceVer.VerNumber(map.get("cartId"));
            cart=cartMapper.selectByPrimaryKey(cartId);
            if(cart!=null){
                int orderUserNoId=cart.getUsernoId()==null?0:cart.getUsernoId();
                int cartStatus=cart.getCartStatus()==null?0:cart.getCartStatus();
                String orderNo=cart.getOrderNo()==null?"":cart.getOrderNo();
                if(cartStatus==501&&orderUserNoId==userNoId){
                    cart.setIsDelete(1);
                    cart.setUsernoId(userNoId);
                    res=cartMapper.updateByPrimaryKeySelective(cart);
                }else {
                    return true;
                }
            }else {
                return false;
            }
            if(res>0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }


    @Override
    public Cart selectCartIsExist(Cart record) {
        return cartMapper.selectCartIsExist(record);
    }

    @Override
    public List<CartList> selectCartList(Cart cart) {
        return cartMapper.selectCartList(cart);
    }

    @Override
    public List<CartList> selectCartList(Map<String, String> map, HttpServletRequest request) throws  Exception{
        Integer usernoId=0;
        Integer res=0;
        Cart cart=new Cart();
        List<CartList> cartList=new ArrayList<>();
        if(map!=null){
            usernoId= InterfaceVer.VerNumber(map.get("userNoId"));
            cart=new Cart();
            cart.setCartStatus(501);
            cart.setUsernoId(usernoId);
            cartList=cartMapper.selectCartList(cart);
            if(cartList!=null&&cartList.size()>0){
                return cartList;
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    @Override
    public List<CartList> selectCartListSureOrder(int usernoId, List<String> cartIdsList) {
        return cartMapper.selectCartListSureOrder(usernoId,cartIdsList);
    }

    @Override
    public int upCartStatus(String  orderNo, List<String> cartIdsList) {
        return cartMapper.upCartStatus(orderNo,cartIdsList);
    }

    @Override
    public List<Cart> selectListByOrderNo(String orderNo) {
        return cartMapper.selectListByOrderNo(orderNo);
    }

    @Override
    public int updateLogByOrderNo(String logisticalNo, List<String> orderList) {
        return cartMapper.updateLogByOrderNo(logisticalNo,orderList);
    }

    @Override
    public int updateStateByOrderNo(List<String> orderList) {
        return cartMapper.updateStateByOrderNo(orderList);
    }
}
